//
//  HelpViewController.swift
//  AutoPrint
//
//  Created by David on 2017/1/15.
//  Copyright © 2017年 fatcat. All rights reserved.
//

import UIKit

class HelpViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView:UITableView!
    
    let listTitles = ["功能介绍","帮助","反馈"]
    
    let copyRightText = "Copyright© 2015-2017 AutoPrint. All Right Reserved."
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "关于\(Fatcat.PRODUCT_NAME)"
        
        // 下一个视图的返回键将显示 返回
        let backItem = UIBarButtonItem()
        backItem.title = "返回"
        navigationItem.backBarButtonItem = backItem
        
        tableView = UITableView(frame: view.frame, style: .Grouped)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        
        let fakeNavBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 64))
        view.addSubview(fakeNavBar)
        fakeNavBar.barTintColor = Fatcat.THEME_COLOR
        fakeNavBar.barStyle = .Black
        fakeNavBar.tintColor = UIColor.whiteColor()
        let fakeNavItem = UINavigationItem(title: title!)
        let fakeBackButton = UIBarButtonItem()
        fakeBackButton.title = "    返回"
        fakeNavItem.leftBarButtonItem = fakeBackButton
        fakeNavBar.pushNavigationItem(fakeNavItem, animated: false)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.navigationBar.hidden = false
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 0 && indexPath.row == 0{
            return 150
        }
        return 44
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Default, reuseIdentifier: "helo")
        
        if indexPath.section == 0{
            if indexPath.row == 0{
                
                let imageView = UIImageView(frame: CGRect(x: 0, y: 8, width: 72, height: 72))
                imageView.center.x = view.center.x
                imageView.backgroundColor = UIColor.grayColor()
                imageView.image = UIImage(named: "AppIcon60x60")
                imageView.layer.cornerRadius = 8
                imageView.layer.masksToBounds = true
                cell.addSubview(imageView)
                
                let nameLabel = UILabel(frame: CGRect(x: 16, y: 88, width: view.frame.width - 32, height: 20))
                nameLabel.font = UIFont.boldSystemFontOfSize(16)
                nameLabel.text = "\(Fatcat.PRODUCT_NAME)"
                nameLabel.textAlignment = .Center
                cell.addSubview(nameLabel)
                
                let versionLabel = UILabel(frame: CGRect(x: 16, y: 106, width: view.frame.width - 32, height: 20))
                versionLabel.textColor = UIColor.lightGrayColor()
                versionLabel.font = UIFont.systemFontOfSize(14)
                versionLabel.text = NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as? String
                versionLabel.textAlignment = .Center
                cell.addSubview(versionLabel)
                
                let copyRightLabel = UILabel(frame: CGRect(x: 16, y: 124, width: view.frame.width - 32, height: 20))
                copyRightLabel.text = copyRightText
                copyRightLabel.textAlignment = .Center
                copyRightLabel.font = UIFont.systemFontOfSize(12)
                copyRightLabel.textColor = UIColor.lightGrayColor()
                cell.addSubview(copyRightLabel)
                
            }else{
                let label = UILabel(frame: CGRect(x: 16, y: 8, width: view.frame.width - 32, height: 28))
                cell.addSubview(label)
                
                label.text = listTitles[indexPath.row - 1]
                cell.accessoryType = .DisclosureIndicator
            }
        }
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.section == 0{
            switch indexPath.row {
            case 1:
                hidesBottomBarWhenPushed = true
                navigationController?.pushViewController(HelpWebViewController(title: listTitles[indexPath.row - 1], url: NSURL(string: "http://baidu.com")!), animated: true)
                break
            default:
                break
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
